// =================================================================
// File: cat.cpp
// Desc: Contains function for displaying the contents of a file
// =================================================================

// =================================================================
// INCLUDES
// =================================================================

#include <iostream>
#include <string>
#include "FSManager.h"
#include "File.h"
#include "Inode.h"
#include "commands.h"
#include "config.h"

using namespace std;

// =================================================================
// FUNCTIONS
// =================================================================

// =================================================================
// Name: cat
// Desc: Outputs contents of file in the filesystem
// Args: string filename - name of file to view contents of
//       FSManager* fsm - pointer to FSManager object
// Rets: 0 on success
// =================================================================
int cat(string filename, FSManager *fsm){

    // Use implemented open, read, and close functions
	int fd = open(filename,"r",fsm);

    File *file = fsm->GetOpenFile(fd);

    Inode *node = fsm->ReadInode(file->GetInodeIndex());

    string data = read(fd,node->GetSize(),fsm);
    
	// Have to append null character at end for printing to console
	data += '\0';

    cout << data;

    close(fd,fsm);

    return 0;
}
